package game.module.item.logic;

import game.common.response.PrompMsg;
import game.common.response.TipMsg;
import game.constants.GameConstant;
import game.constants.Popsinger;
import game.constants.Position;
import game.module.item.beans.CharacterGoods;
import game.module.item.beans.Goodmodel;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

@Component
public class BodyGoodsContiner extends AbstractContainer implements IBody {
	private static final Logger logger = Logger.getLogger(BodyGoodsContiner.class);

	@Autowired
	private CacheManager cacheManager;

	public BodyGoodsContiner() {
		beginPosition = Position.BodyGoodsBeginPostion;
	}

	public void initEquipPropertiesToCharacter() {
		// try {
		// for (CharacterGoods goods : getGoodsList()) {
		// owner.getEquipmentController().changeProperty(owner, goods, TakeMethod.on);
		// if (goods.getPosition() == Position.POSTION_TESHU && goods.getIsUse() == 1) {
		// owner.getPropertyAdditionController().addChangeListener(goods);
		// }
		// }
		// } catch (Exception e) {
		// logger.error(e.getMessage(), e);
		// }
	}

	@Override
	public boolean checksuit(CharacterGoods fromGoods) {
		return checksuit(fromGoods, true);
	}

	public boolean checksuit(CharacterGoods fromGoods, boolean sendsuitmsg) {
		// 条件包含 佩戴位置正确;道具过期;等级门派限制;潜能加点限制;
		Goodmodel goodmodel = fromGoods.getGoodModel();
		int topostion = goodmodel.getPosition();

		if (topostion < Position.BodyGoodsBeginPostion || topostion > Position.BodyGoodsEndPostion) {
			if (sendsuitmsg) {
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 756));
			}
			return false;
		}
		if (goodmodel.getPopsinger() != 0 && goodmodel.getPopsinger() != owner.getCareerId()) {
			if (sendsuitmsg) {
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 1006, Popsinger.getPopsingerName(goodmodel.getPopsinger())));
			}
			return false;
		}
		if (goodmodel.getLimitGrade() > owner.getGrade()) {
			if (sendsuitmsg) {
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 1007, goodmodel.getLimitGrade() + ""));
			}

			return false;
		}
		if (goodmodel.getAttackAddpoint() > owner.getAttackAddpoint()) {
			if (sendsuitmsg) {
				//
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 1008, goodmodel.getAttackAddpoint() + ""));
			}
			return false;
		}
		if (goodmodel.getDefenceAddpoint() > owner.getDefenceAddpoint()) {
			if (sendsuitmsg) {
				//
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 1009, goodmodel.getDefenceAddpoint() + ""));
			}
			return false;
		}
		if (goodmodel.getStrongAddpoint() > owner.getStrongAddpoint()) {
			if (sendsuitmsg) {
				//
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 1010, goodmodel.getDefenceAddpoint() + ""));
			}
			return false;
		}
		if (goodmodel.getLightAddpoint() > owner.getLightAddpoint()) {
			if (sendsuitmsg) {
				//
				owner.sendMsg(new PrompMsg(TipMsg.MSGPOSITION_ERRORTIP, 1011, goodmodel.getDefenceAddpoint() + ""));
			}
			return false;
		}
		return true;
	}

	@Override
	protected void onChangePositionGoodsAdd(CharacterGoods goods) {
		// if (goods.getGoodModel().isShowInAvator()) {
		// owner.getEyeShotManager().sendMsg(new AvatarChange60000(owner, goods.getGoodModel(), true));
		// }
		// if ((goods.getPingzhiColor() == 4 && goods.getBind() == 0) || (goods.getBind() == 0 && goods.getGoodModel().getBinding() == BindChangeType.DRESSBIND)) {
		// owner.sendMsg(new GoodsDelete10178(goods.getPosition(), 0));
		// goods.setBind(CommonUseNumber.byte1);
		// GoodsDataEntryManager.getInstance().asynUpdataCharacterGoods(owner, goods);
		// owner.sendMsg(new BodyGoodsUpdata10180(goods));
		// }
		// owner.getEquipmentController().changeProperty(owner, goods, TakeMethod.on);

	}

	@Override
	protected void onChangePostionGoodsRemove(CharacterGoods goods) {
		// if (goods.getGoodModel().isShowInAvator()) {
		// owner.getEyeShotManager().sendMsg(new AvatarChange60000(owner, goods.getGoodModel(), false));
		// }
		// owner.getEquipmentController().changeProperty(owner, goods, TakeMethod.off);
	}

	@Override
	protected void beforeGetGoods(CharacterGoods fromGoods) {
		fromGoods.setInHorseId(0);
	}

	@Override
	public short getSuitPostionForGoods(int heroId, CharacterGoods fromGoods, IContainer fromContainer) {
		return fromGoods.getGoodModel().getPosition();
	}

	@Override
	protected void onGoodsAdd(int heroId, CharacterGoods goods, int totalCount) {
	}

	@Override
	protected void onGoodsRemove(CharacterGoods goods) {
	}

	@Override
	protected void setGoodsMap(Map<Short, CharacterGoods> goods) {

	}

	@SuppressWarnings("unchecked")
	@Override
	protected Map<Short, CharacterGoods> getGoodsMap(int heroId) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS);
		return (Map<Short, CharacterGoods>) cache.get(GameConstant.CACHE_GOODS_BODY + heroId);
	}

	@Override
	public int getTotalCapacity(int heroId) {
		return 13;
	}

}
